package com.app.controller.backstage;

import com.app.model.*;
import com.app.service.*;
import com.app.util.CommonHelp;
import com.app.util.DateUtil;
import com.app.util.Result;
import com.app.util.TokenUtils;
import com.app.vo.LivePortendVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@Controller
public class LivePortendController {
    @Autowired
    LivePortendService livePortendService;
    @Autowired
    UserService userService;
    @Autowired
    AdminUserService adminUserService;

    @Autowired
    EnterpriseService enterpriseService;
    @Autowired
    ComRecruitersService comRecruitersService;
    @Autowired
    JobPublishService jobPublishService;


    /**
     * 直播预告列表
     */
    @GetMapping("/getLivePortendList")
    @ResponseBody
    public Result<Object> getLivePortendList(HttpServletRequest request, LivePortendModel model) {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405,"token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if(user==null)
        {
            return Result.error(405,"用户不存在");
        }

        Page<LivePortendModel> modelPage = livePortendService.modelPage(model);
        LivePortendVo vo = new LivePortendVo();
        for (int i = 0; i < modelPage.getRecords().size(); i++) {
            SimpleDateFormat simpleDateFormatFair = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            Timestamp data1 = Timestamp.valueOf(String.valueOf(modelPage.getRecords().get(i).getStartTime()));//开始时间
            Timestamp data2 = Timestamp.valueOf(String.valueOf(modelPage.getRecords().get(i).getEndTime()));//结束时间
            String time = simpleDateFormatFair.format(data1);
            String time2 = simpleDateFormatFair.format(data2);
            modelPage.getRecords().get(i).setStartTime(time);
            modelPage.getRecords().get(i).setEndTime(time2);
            if(modelPage.getRecords().get(i).getStatus()==1)
            {
                modelPage.getRecords().get(i).setStatusText("直播中");
            }else if(modelPage.getRecords().get(i).getStatus()==2)
            {
                modelPage.getRecords().get(i).setStatusText("待开启");
            }else {
                modelPage.getRecords().get(i).setStatusText("已结束");
            }
        }
        vo.setList(modelPage.getRecords());

        vo.setCount((int)modelPage.getTotal());
        return Result.success(vo);
    }

    /**
     * 根据广告ID查询
     */
    @GetMapping("/geLivePortendById")
    @ResponseBody
    public Result<Object> geLivePortendById(HttpServletRequest request,
                                               Integer id) {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405,"token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if(user==null)
        {
            return Result.error(405,"用户不存在");
        }

        return Result.success(livePortendService.getModelById(id));
    }

    /**
     * 获取全部企业
     */
    @GetMapping("/getComList")
    @ResponseBody
    public Result<Object> getComList(HttpServletRequest request) {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405,"token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if(user==null)
        {
            return Result.error(405,"用户不存在");
        }
        return Result.success(enterpriseService.findModelByName(""));
    }

    /**
     * 查询企业HR
     */
    @GetMapping("/getComUserList")
    @ResponseBody
    public Result<Object> getComUserList(HttpServletRequest request,String comIds) {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405,"token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if(user==null)
        {
            return Result.error(405,"用户不存在");
        }
        String str[] = comIds.split(",");
        List<ComRecruitersModel> modelList = comRecruitersService.getComRecruitersByComId(str);
        for (int i = 0; i < modelList.size(); i++) {
            EnterpriseModel enterpriseModel = enterpriseService.findModelById(modelList.get(i).getComId());
            UserModel userModel = userService.getUserByRId( modelList.get(i).getId());
            if(userModel!=null)
            {
                modelList.get(i).setUserId(userModel.getId());
                modelList.get(i).setComName(enterpriseModel.getBrandName());
            }

        }
        return Result.success(modelList);
    }

    /**
     * 查询企业岗位
     */
    @GetMapping("/getComPublishList")
    @ResponseBody
    public Result<Object> getComPublishList(HttpServletRequest request,String comIds) {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405,"token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if(user==null)
        {
            return Result.error(405,"用户不存在");
        }
        String str[] = comIds.split(",");
        List<JobPublishModel> modelList = jobPublishService.getComPublishByComId(str);
        for (int i = 0; i < modelList.size(); i++) {

            EnterpriseModel enterpriseModel = enterpriseService.findModelById(modelList.get(i).getEnrollId());
            modelList.get(i).setComName(enterpriseModel.getBrandName());
        }
        return Result.success(modelList);
    }

    /**
     * 编辑直播预告
     */
    @PostMapping("/editLivePortend")
    public Result editLivePortend(HttpServletRequest request, @RequestBody LivePortendModel model) {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405,"token不存在");
        }
        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if(user==null)
        {
            return Result.error(405,"用户不存在");
        }
        model.setStatus(2);
        Date start = DateUtil.transferString2Date(model.getStartTime());
        Date end = DateUtil.transferString2Date(model.getEndTime());
        Date thisTime = CommonHelp.getNowTimestamp();
        if(start.getTime()<thisTime.getTime()||end.getTime()<thisTime.getTime())
        {
            return Result.error(201,"开播时间请选择大于当前时间");
        }
        if(model.getId()!=null&&model.getId()>0)
        {
            livePortendService.update(model);
        }else {
            livePortendService.insert(model);
        }

        return Result.success("操作成功");
    }

    /**
     * 删除直播预告
     */
    @PostMapping("/deleteLivePortend")
    @ResponseBody
    public Result<Object> deleteLivePortend(HttpServletRequest request,Integer id) {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405,"token不存在");
        }
        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if(user==null)
        {
            return Result.error(405,"用户不存在");
        }
        livePortendService.delete(id);
        return Result.success("操作成功");
    }


}
